package com.metaera.app.controller.presenter;

import android.content.Context;

import com.metaera.app.controller.core.RequestPresenter;
import com.metaera.app.controller.view.core.INetworkRequestView;
import com.metaera.app.data.DataRepository;
import com.metaera.app.data.model.BasicResponse;
import com.metaera.app.data.model.UploadImageResponse;
import com.metaera.app.data.model.UserDetailsResponse;
import com.metaera.app.data.rx.ResponseObserver;
import com.metaera.app.data.rx.SchedulersTransformer;
import com.metaera.app.utils.event.UserEditEvent;

import org.greenrobot.eventbus.EventBus;

import java.io.File;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.functions.Function;
import top.zibin.luban.Luban;

/**
 * 更改用户信息
 */
public class UserEditPresenter extends RequestPresenter<INetworkRequestView<String>> {

    public void updateAvatar(String path, Context context) {

        Observable.just(path)
                // 压缩
                .map(s -> Luban.with(context).load(new File(path)).ignoreBy(100).get(path))
//                .map(s -> new File(path))

                // 上传图片
                .flatMap((Function<File, ObservableSource<BasicResponse<UploadImageResponse>>>) file ->
                        DataRepository.getInstance().upload(file))

                // 修改用户头像
                .flatMap((Function<BasicResponse<UploadImageResponse>, ObservableSource<BasicResponse<Object>>>) response ->
                        DataRepository.getInstance()
                                .editUserInfo(
                                        null,
                                        response.getData().getUrl(),
                                        null,
                                        null,
                                        null,
                                        null,
                                        null,
                                        null,
                                        null,
                                        null,
                                        null,
                                        null,
                                        null,
                                        null
                                )
                )
                .compose(SchedulersTransformer.transformer(mView))
                .subscribe(new ResponseObserver<>(this, mView, entity -> {
                    mView.successful(entity.getMessage());
                    EventBus.getDefault().post(new UserEditEvent());
                }));
    }


    public void update(UserDetailsResponse user) {
        DataRepository.getInstance()
                .editUserInfo(
                        user.getNickname(),
                        null,
                        String.valueOf(user.getSex()),
                        user.getPosition(),
                        user.getDesc(),
                        user.getWeibo(),
                        user.getOfficial_account(),
                        user.getTwitter(),
                        user.getEmail(),
                        String.valueOf(user.getIs_show_phone()),
                        user.getPhone(),
                        user.getKnowledge_planet(),
                        String.valueOf(user.getIs_show_wechat()),
                        user.getWechat()
                )
                .compose(SchedulersTransformer.transformer())
                .subscribe(new ResponseObserver<>(
                                this,
                                mView,
                                response -> {
                                    mView.successful(response.getMessage());
                                    EventBus.getDefault().post(new UserEditEvent());
                                }
                        )
                );
    }
}
